import { Button, Color, instantiate, math, Node, NodeEventType, Prefab, resources, SkeletalAnimation, Skeleton, sp, Tween, tween, UITransform, Vec3 } from "cc";
import { BattleRole } from "../../new/role/BattleRole";
import { ResMgr } from "../../new/common/ResMgr";
import { BattlePos } from "../../new/common/BattlePos";
import { TopBarUI } from "./TopBarUI";
import EventMgr from "../../new/event/EventMgr";
import { EventId } from "../../new/event/EventId";
import { UITriggerMgr } from "../UITrigger/UITriggerMgr";
import { MoveComp } from "./components/MoveComp";
import { AniComp } from "./components/AniComp";
import { HpComp } from "./components/HpComp";
import { AttrType } from "../../new/attr/AttrType";

export class BattleRoleUI extends Node {

    public battleRole: BattleRole;

    private initPos: Vec3;

    private face: number;
    private faceVec: Vec3 = new Vec3(1, 1, 1);

    private clickSp: Node;

    public UITriggerMgr: UITriggerMgr

    private moveComp: MoveComp;
    private aniComp: AniComp;
    private hpComp: HpComp;

    init(battleRole: BattleRole) {
        this.battleRole = battleRole;
        this.UITriggerMgr = new UITriggerMgr();
        this.moveComp = this.addComponent(MoveComp);
        this.aniComp = this.addComponent(AniComp)
        this.aniComp.init(battleRole.roleData);
        this.hpComp = this.addComponent(HpComp);
        this.hpComp.init()

        let pos = BattlePos[battleRole.roleData.pos]
        this.initPos = new Vec3(pos[0], pos[1], 0);
        this.setPosition(this.initPos);
        this.setDir(battleRole.roleData.face);

        this.clickSp = new Node();
        this.clickSp.addComponent(UITransform).setContentSize(new math.Size(100, 200));
        this.clickSp.setPosition(new Vec3(0, 70, 0));
        this.addChild(this.clickSp);
        this.clickSp.on(NodeEventType.TOUCH_END, this.onClick, this)
    }

    onClick(): void {
        console.log("被点击")
        EventMgr.ins.event(EventId.SELECT_TARGET, this.battleRole)
    }

    /**设置行动表现-高亮之类的 */
    action(): void {
        this.hpComp.hpBar.showArrow()
    }

    actionEnd() {
        this.hpComp.hpBar.hidArrow()
    }

    /**做动作，取消行动表现 */
    doAction(): void {

    }

    stopWalk(): void {
        this.moveComp?.stopMove();
    }

    goBack(): void {
        // this.walkToPoint(this.initPos)
        this.setPosition(this.initPos)
    }

    walkToPoint(point: Vec3, dic: number = 0, callBack: Function = null): void {
        this.aniComp.playAni("move", true)
        this.moveComp?.moveToPoint(point, dic, callBack)
    }

    playAni(ani: string) {
        this.aniComp.playAni(ani)
    }

    /**攻击造成伤害 */
    atkDamage(): void {

    }

    /**受击表现 */
    onHit(hitType: number = 0): void {
        if (hitType == 0) {//普通受击
            this.aniComp.playAni("behit")
            console.log("被攻击")
        } else if (hitType == 1) {//暴击受击
            this.aniComp.playAni("behit")
            console.log("被攻击")
        } else if (hitType == 2) {//格挡受击
            this.aniComp.playAni("behit")
        }
    }

    /**闪避表现 */
    onMiss(): void {
        this.aniComp.playAni("behit")
        console.log("闪避")
    }

    /**更新血量 */
    updateHp(num: number): void {
        let hp = this.battleRole.attrMgr.getAttr(AttrType.hp);
        let maxHp = this.battleRole.attrMgr.getAttr(AttrType.maxHp);
        this.hpComp.setValue(hp / maxHp)
    }

    isDie(): boolean {
        let hp = this.battleRole.attrMgr.getAttr(AttrType.hp);
        return hp <= 0;
    }

    onDie(): void {
        this.aniComp.playAni("death")
        console.log("死亡")
    }

    onBleed(): void {
        this.aniComp.playBleed()
    }

    /**设置朝向 */
    setDir(face: number) {
        this.face = face;
        this.faceVec.x = face
        this.setScale(this.faceVec)
    }
}